package com.primb.androidlibs.utils;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by Chen on 2016/12/7.
 * 功能描述：数据格式化工具类
 */

public final class FormatUtils {

    public static final DecimalFormat FLOAT_FORMAT = new DecimalFormat("###,###,###,##0.0");
    public static final DecimalFormat DOUBLE_FORMAT = new DecimalFormat("###,###,###,##0.00");

    public static final DecimalFormat INT_FORMAT = new DecimalFormat("###,###,###,###");

    public static final DecimalFormat FORMAT = new DecimalFormat("###,###,###,##0.0000");

    public static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd");

    public static final int YEAR = 0x12;
    public static final int MONTH = 0x13;
    public static final int DAY = 0x14;
    public static final int HOUR = 0x15;
    public static final int MINUTES = 0x16;

    public static String formatDate(Date date, String flag) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy" + flag + "MM" + flag + "dd");
        String dateStr = dateFormat.format(date);
        return dateStr;
    }

    public static String formatDate(String date) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
        String dateStr;
        try {
            dateStr = DATE_FORMAT.format(dateFormat.parse(date));
        } catch (Exception e) {
            dateStr = date;
        }
        return dateStr;
    }

    public static String formatDate(String date, String old, int time, String newFlag) {
        SimpleDateFormat dateFormat;
        SimpleDateFormat dateNew = null;
        switch (time) {
            case YEAR:
                dateFormat = new SimpleDateFormat("yyyy");
                dateNew = new SimpleDateFormat("yyyy");
                break;
            case MONTH:
                dateFormat = new SimpleDateFormat("yyyy" + old + "MM");
                dateNew = new SimpleDateFormat("yyyy" + newFlag + "MM");
                break;
            case DAY:
                dateFormat = new SimpleDateFormat("yyyy" + old + "MM" + old + "dd");
                dateNew = new SimpleDateFormat("yyyy" + newFlag + "MM" + newFlag + "dd");
                break;
            case HOUR:
                dateFormat = new SimpleDateFormat("yyyy" + old + "MM" + old + "dd HH");
                dateNew = new SimpleDateFormat("yyyy" + newFlag + "MM" + newFlag + "dd HH");
                break;
            case MINUTES:
                dateFormat = new SimpleDateFormat("yyyy" + old + "MM" + old + "dd HH:mm");
                dateNew = new SimpleDateFormat("yyyy" + newFlag + "MM" + newFlag + "dd HH:mm");
                break;
            default: {
                dateFormat = new SimpleDateFormat("yyyyMMdd");
                dateNew = dateFormat = new SimpleDateFormat("yyyyMMdd");
            }
        }
        String dateStr;
        try {
            dateStr = dateNew.format(dateFormat.parse(date));
        } catch (Exception e) {
            dateStr = date;
        }
        return dateStr;
    }


    public static String formatDBDate(String date) {
        String replace = date.replace("2016", "") + "月";
        return replace;
    }

    public static String formatHttpDate(Date date) {
        if (date == null) {
            date = new Date();
        }
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:SS");
        String replace = dateFormat.format(date);
        return replace;
    }

    public static String value2Percent(Object obj) {
        return FORMAT.format(convertToDouble(obj, 0d) * 100);
    }

    public static String percent2Value(Object obj) {
        if (convertToDouble(obj, 0d) == 0) {
            return 0 + "";
        }
        return FORMAT.format(convertToDouble(obj, 0d) / 100);
    }

    public static String format(Object obj, int count) {
        StringBuffer stringBuffer = new StringBuffer("###,###,###,##0");
        if (count < 0) {
            count = 0;
        }
        for (int i = 0; i < count; i++) {
            if (i == 0) {
                stringBuffer.append(".");
            }
            stringBuffer.append("0");
        }
        DecimalFormat decimalFormat = new DecimalFormat(stringBuffer.toString());
        return decimalFormat.format(convertToDouble(obj));
    }

    public static String format2Double(Object obj) {
        return DOUBLE_FORMAT.format(convertToDouble(obj, 0d));
    }

    public static String format2Float(Object obj) {
        return FLOAT_FORMAT.format(convertToFloat(obj, 0f));
    }

    public static String format2Int(Object obj) {
        return INT_FORMAT.format(convertToInt(obj, 0));
    }


    public static int convertToInt(Object obj, int defaultValue) {
        if (obj == null || "".equals(obj.toString().trim())) {
            return defaultValue;
        }
        try {
            return Integer.valueOf(obj.toString());
        } catch (Exception e) {
            return defaultValue;
        }
    }

    public static double convertToDouble(Object obj, double defaultValue) {
        if (obj == null || "".equals(obj.toString().trim())) {
            return defaultValue;
        }
        try {
            return Double.valueOf(obj.toString());
        } catch (Exception e) {
            return defaultValue;
        }
    }

    public static float convertToFloat(Object obj, float defaultValue) {
        if (obj == null || "".equals(obj.toString().trim())) {
            return defaultValue;
        }
        try {
            return Float.valueOf(obj.toString());
        } catch (Exception e) {
            return defaultValue;
        }
    }

    public static int convertToInt(Object obj) {
        return convertToInt(obj, 0);
    }

    public static double convertToDouble(Object obj) {
        return convertToDouble(obj, 0d);
    }

    public static float convertToFloat(Object obj) {
        return convertToFloat(obj, 0f);
    }


}
